home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 May / macformat-037.iso / Shareware City / Science / CFG 2.3 (Shareware) / Palettes / Convert "Image" Palette (v2.0) / convert image.c next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  5.4 KB  |  247 lines  |  [TEXT/MPCC]

  1. /*
  2.     This file was used to convert "Image" palettes into CFG palettes.
  3. */
  4.  
  5.  
  6. #include <string.h>
  7.  
  8.  
  9. typedef struct identifier_struct    // 128 byte header
  10. {
  11.     char    zero[4];            // first 4 bytes must be 0
  12.     char    appl_name[32];        // application name
  13.     char    by[32];
  14.     char    file_type[8];        // fractal, palette, cusfrac, etc.
  15.     char    version[4];            // application version: x.xx
  16.     char    earliest_vers[4];    // earliest app version that can open file
  17.     char    registered[4];
  18.     char    password[8];
  19.     char    reserved[32];
  20. } identifier_struct;
  21.  
  22. typedef struct file_palette_header
  23. {
  24.     long    entries;        // number of entries in palette
  25.     Str255    load_pal_name;    // name of palette
  26. } file_palette_header;
  27.  
  28. typedef struct image_palette
  29. {
  30.     short            entries;
  31.     unsigned char    red[32];
  32.     unsigned char    green[32];
  33.     unsigned char    blue[32];
  34.     RGBColor        color[240];
  35. } image_palette;
  36.  
  37.  
  38. static image_palette    pal;
  39. static char             file_cmp[4] = { '2', '.', '0', '0' };
  40. static char             file_erl[4] = { '2', '.', '0', '0' };
  41.  
  42.  
  43. #define FILE_ID_PALETTE            "palette"
  44. #define FILE_APPL_NAME            "Color Fractal Generator"
  45. #define FILE_BY                    "John A. Schlack   Shadowbane"
  46.  
  47.  
  48. /* --------------------------------------------------------------------------------- */
  49.  
  50.  
  51. void main( void );
  52. Boolean GetPaletteFromFile( char * fname, short vnum );
  53. void _pstrcpy( unsigned char * dest, unsigned char * src );
  54. void pstrcat( unsigned char * dest, unsigned char * src );
  55. void WritePaletteToFile( char * name, short vnum );
  56.  
  57.  
  58. /* --------------------------------------------------------------------------------- */
  59.  
  60.  
  61. void main( void )
  62. {
  63.     Point        thePoint = {-1, -1};
  64.     SFReply        reply;
  65.     SFTypeList    typeList;
  66.     char        name[64];
  67.     
  68.     InitGraf( &thePort );
  69.     InitFonts();
  70.     FlushEvents( everyEvent, 0 );
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs( 0L );
  75.     InitCursor();
  76.     
  77.     typeList[0] = 'ICOL';
  78.     for (;;)
  79.     {
  80.         SFGetFile( thePoint, 0L, 0L, 1, typeList, 0L, &reply );
  81.         if (!(reply.good)) break;
  82.         _pstrcpy( (unsigned char *) name, reply.fName );
  83.         pstrcat( (unsigned char *) name, "\p.cfg" );
  84.         if (GetPaletteFromFile( (char *) (reply.fName), reply.vRefNum ))
  85.             WritePaletteToFile( name, reply.vRefNum );
  86.     }
  87. }
  88.  
  89.  
  90. /* --------------------------------------------------------------------------------- */
  91.  
  92.  
  93. Boolean GetPaletteFromFile( char * fname, short vnum )
  94. {
  95.     short    f;
  96.     long    size;
  97.     Boolean    res;
  98.     char    head[32];
  99.     OSErr    theErr;
  100.     
  101.     if (FSOpen( fname, vnum, &f ) != noErr)
  102.         return false;
  103.     size = 32L;
  104.     if (FSRead( f, &size, head ) != noErr)
  105.     {
  106.         FSClose( f );
  107.         return false;
  108.     }
  109.     pal.entries = (short) (head[0]);
  110.     size = 32L;
  111.     theErr = FSRead( f, &size, pal.red );
  112.     size = 32L;
  113.     theErr = FSRead( f, &size, pal.green );
  114.     size = 32L;
  115.     theErr = FSRead( f, &size, pal.blue );
  116.     FSClose( f );
  117.     return true;
  118. }
  119.  
  120.  
  121. /* --------------------------------------------------------------------------------- */
  122.  
  123.  
  124. void _pstrcpy( dest, src )
  125.  
  126. unsigned char *    dest;
  127. unsigned char *    src;
  128.  
  129. {
  130.     asm {
  131.             movea.l    dest,a1        ; use movea instead of lea since dest and src are
  132.             movea.l    src,a0        ; already addresses
  133.             clr.w    d0            ; clear low word of d0 for loop constant
  134.             move.b    (a0),d0        ; want to copy *src + 1 bytes
  135.     @loop    move.b    (a0)+,(a1)+    ; copy a byte
  136.             dbra    d0,@loop
  137.         }
  138. }
  139.  
  140.  
  141. /* -------------------------------------------------------------------------------- */
  142.  
  143.  
  144. void pstrcat( dest, src )
  145.  
  146. unsigned char *    dest;
  147. unsigned char *    src;
  148.  
  149. {
  150.     unsigned char * q = dest + (*dest) + 1;        // move to open space
  151.     short            cnt, i;
  152.     
  153.     if ((*dest + *src) > 255)
  154.         cnt = 255 - *dest;
  155.     else
  156.         cnt = *src;
  157.     
  158.     src++;                    // move beyond size
  159.     for (i=0; i<cnt; i++)
  160.         *q++ = *src++;
  161.     (*dest) += cnt;
  162. }
  163.  
  164.  
  165. /* --------------------------------------------------------------------------------- */
  166.  
  167.  
  168. void WritePaletteToFile( char * name, short vnum )
  169. {
  170.     identifier_struct    ID;
  171.     file_palette_header    pal_head;
  172.     OSErr                theErr;
  173.     short                f, start, i, first;
  174.     long                size;
  175.     char                r, g, b;
  176.     double                f1, f2;
  177.     
  178.     theErr = Create( name, vnum, 'fraG', 'fraP');
  179.     if ((theErr != noErr) && (theErr != dupFNErr))
  180.         return;
  181.  
  182.     theErr = FSOpen( name, vnum, &f );
  183.     if (theErr != noErr)
  184.     {
  185.         FSDelete( name, vnum );
  186.         return;
  187.     }
  188.  
  189.     r = pal.red[0]; g = pal.green[0]; b = pal.blue[0];
  190.     for (i=1, start=0; i<pal.entries; i++)
  191.     {
  192.         if ((r == pal.red[i]) && (g == pal.green[i]) && (b == pal.blue[i]))
  193.             start++;
  194.         else
  195.             break;
  196.     }
  197.     
  198.     if (i >= pal.entries)
  199.     {
  200.         FSClose( f );
  201.         FSDelete( name, vnum );
  202.         return;
  203.     }
  204.     
  205.     for (i=0; i<240; i++)
  206.     {
  207.         f1 = (double) (pal.entries - start - 1) * i / 239.0 + (double) start;
  208.         first = (short) f1;
  209.         f1 = 1.0 - (f1 - (double) first);
  210.         f2 = 1.0 - f1;
  211.         pal.color[i].red = ((unsigned short) ((double) pal.red[first] * f1 +
  212.             (double) pal.red[first+1] * f2)) << 8;
  213.         pal.color[i].green = ((unsigned short) ((double) pal.green[first] * f1 +
  214.             (double) pal.green[first+1] * f2)) << 8;
  215.         pal.color[i].blue = ((unsigned short) ((double) pal.blue[first] * f1 +
  216.             (double) pal.blue[first+1] * f2)) << 8;
  217.     }
  218.     
  219.     strcpy( ID.appl_name, FILE_APPL_NAME );
  220.     strcpy( ID.by, FILE_BY );
  221.     strcpy( ID.file_type, FILE_ID_PALETTE );
  222.     memcpy( ID.version, file_cmp, 4L );
  223.     memcpy( ID.earliest_vers, file_erl, 4L );
  224.  
  225.     pal_head.entries = 240L;
  226.     _pstrcpy( pal_head.load_pal_name, (unsigned char *) name );
  227.     
  228.     // WRITE ID & PALETTE
  229.     
  230.     size = (long) sizeof( identifier_struct );
  231.     FSWrite( f, &size, &ID );
  232.     size = (long) sizeof( file_palette_header );
  233.     FSWrite( f, &size, &pal_head );
  234.     for (i=0; i<240; i++)
  235.     {
  236.         size = (long) sizeof( RGBColor );
  237.         FSWrite( f, &size, &(pal.color[i]) );
  238.     }
  239.         
  240.     // CLEAN UP
  241.     
  242.     GetFPos( f, &size );
  243.     SetEOF( f, size );
  244.     FSClose( f );
  245.     FlushVol(0L, vnum);
  246. }
  247.